<?php

/**
 * Criado em 09/10/2009
 * CursoDAO.php
 */
 class CursoDAO {

 	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return Curso
	 */
	public function load($id){
		$sql = 'SELECT * FROM curso WHERE codCurso = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM curso';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM curso ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}

	/**
	 * Read row
	 *
	 * @return Curso
	 */
	protected function readRow($row) {
		$curso = new Curso();

		$curso->codCurso = $row['codCurso'];
		$curso->nomeCurso = $row['nomeCurso'];
		$curso->codCoordenador = $row['codCoordenador'];

		return $curso;
	}

	protected function getList($sqlQuery) {
		$tab = QueryExecutor :: execute($sqlQuery);
		$ret = array ();
		for ($i = 0; $i < count($tab); $i++) {
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}

	/**
	 * Get row
	 *
	 * @return Curso
	 */
	protected function getRow($sqlQuery) {
		$tab = QueryExecutor :: execute($sqlQuery);
		return $this->readRow($tab[0]);
	}

	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery) {
		return QueryExecutor :: execute($sqlQuery);
	}

	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery) {
		return QueryExecutor :: executeUpdate($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery) {
		return QueryExecutor :: executeInsert($sqlQuery);
	}

	/**
	 * listar os cursos do coordenador logado
	 * @param integer codCoordenador codigo do coodenador
	 */
	public function listarCursosPorCoordenador($cod) {
 		$sql = 'SELECT * FROM curso WHERE codCoordenador = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($cod);
		return $this->getList($sqlQuery);
	}

	/**
	 * Obter nome do curso
	 * @param integer codCurso codigo do curso
	 */
	public function obterNomeCurso($cod) {
 		$sql = 'SELECT nomeCurso FROM curso WHERE codCurso = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($cod);
		$tab = $this->execute($sqlQuery);

		$nomeCurso = new Curso();
		$nomeCurso->nomeCurso = $tab[0]["nomeCurso"];
		return $nomeCurso;
	}

 }
?>
